﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneStationFile.Common
{
    public  class EnumHelper
    {
        #region 获取枚举的name值，绑定列表。T为枚举类型
        /// <summary>
        /// 获取枚举的name值，绑定列表。T为枚举类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyDefault"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetEnumListName<T>(string keyDefault ="", string valueDefault = "")
        {
            Dictionary<string, object> dicEnum = new Dictionary<string, object>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            string[] filename = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in filename)
            {
                //string name = string.Empty;
                //var field = enumType.GetField(item);
                //object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                //if (arr != null && arr.Length > 0)
                //{
                //    name = item;   //属性描述  
                //}
                //else
                //{
                //    description = item;  //描述不存在取字段名称  
                //}
                dicEnum.Add(item, (int)Enum.Parse(enumType, item));  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }
        #endregion
        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, object> GetListOfEnum<T>()
        {
            return EnumHelper.GetEnumListName<T>();
        }
    }
}
